package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<TbBusinessTrackRecord> getById(Long id) {
        return tbBusinessTrackRecordMapper.getById(id);
    }

    @Override
    public void add(BusinessTrackVo businessTrackVo) {
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        tbBusiness.setStatus("2");
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusinessMapper.updateTbBusiness(tbBusiness);

        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
        String username = SecurityUtils.getUsername();
        tbBusinessTrackRecord.setCreateBy(username);

        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);
    }

    @Override
    @Transactional
    public void back(Long id, String reson) {
        String dictLabel = sysDictDataMapper.selectDictLabel("reasons_for_business_reporting", reson);

        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        tbBusinessTrackRecord.setBusinessId(id);
        String username = SecurityUtils.getUsername();
        tbBusinessTrackRecord.setCreateBy(username);
        tbBusinessTrackRecord.setRecord(dictLabel);

        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);

        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setId(id);
        tbBusiness.setStatus("4");
        tbBusinessMapper.updateTbBusiness(tbBusiness);
    }


}
